﻿
@{
    ViewBag.Title = "UserDevice";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>用户设备绑定</legend>
</fieldset>
<div style="margin-left:50px;width:100%;">
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">用户</label>
            <div class="layui-input-inline" style="width:300px;">
                <select name="ProjectId" lay-verify="required" lay-search lay-filter="UserSelFilter" id="UserSel">
                    <option value=""></option>
                    @for (int i = 0; i < ViewBag.UserList.Count; i++)
                    {
                        <option value="@ViewBag.UserList[i]["UserId"]">@ViewBag.UserList[i]["UserName"]|@ViewBag.UserList[i]["NickName"]</option>
                    }
                </select>
            </div>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增</button>
            
        </div>
    </form>
</div>
<table id="demo" lay-filter="UserDeviceTableList"></table>

<div id="addLayer" style="display:none;">
    <form class="layui-form" id="AddFrom" style="margin-top:10px;">       
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">搜索</label>
                <div class="layui-input-inline">
                    <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入设备名" class="layui-input">
                    <input type="text" name="sss" value="" style="display:none" />
                </div>
                <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAddSave"><i class="layui-icon">&#xe61f;</i>保存</button>
            </div>
        </div>
        
    </form>
    <table id="demo2" lay-filter="DeviceTableList"></table>
</div>

<div style="font-size:12px;margin-top:10px;margin-bottom:10px;margin-left:15px;width:100%;position: absolute;left: 0px;bottom: 0px;">
    ©2020 德器(重庆)科技有限公司|DEQI(CQ) TECHNOLOGY.CO.,LTD 服务热线 400-9019-505
    @*重庆环元环境科学研究院*@
</div>




<script type="text/javascript">
    var table
    layui.use(['form', 'table', 'upload', 'layer', 'element'], function () {
        var element = layui.element;
        var upload = layui.upload;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;
        var SelUserId = -1;
        var index = -1;


        $("#BtnAdd").on("click", function () {
            if (SelUserId == -1 || SelUserId == "") {
                return;
            }
            index = layer.open({
                type: 1,
                area: ['700px', '600px'],
                content: $("#addLayer") //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        });

        $("#BtnAddSave").on("click", function () {
            var checkStatus = table.checkStatus('demo2');
            var data = checkStatus.data;
            if (data.length != 1) {
                return;
            }
            var DeviceId = data[0].DeviceId;
            $.ajax({
                type: "POST",
                url: "/Panel/UserDeviceAdd",
                async: false,
                data: { "DeviceId": DeviceId, "UserId": SelUserId },
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('添加成功', { icon: 6, time: 1000 });
                        table.reload("demo");
                        layer.close(index);
                        //tab.close(tab.getId());
                    } else {
                        alert(msg.result);
                    }
                }
            });


            
        });

        

        form.on('select(UserSelFilter)', function (data) {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            SelUserId = data.value;
            if (SelUserId != "") {
                table.reload("demo", {
                    where: {
                        SortName: "b.DeviceId", Order: "Desc", Condition: " and c.UserId=" + SelUserId
                    }
                });
            } else {
                table.reload("demo", {
                    where: {
                        SortName: "b.DeviceId", Order: "Desc", Condition: " and c.UserId=-1"
                    }
                });
            }

        });

        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Panel/UserDeviceTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "b.DeviceId", Order: "Desc", Condition: " and c.UserId=" + SelUserId }
            , limits: [10, 50, 100] 
            , cols: [[ //表头
                //{ type: "checkbox" }
                  { field: 'Name', title: '设备名称', width: 200 }
                , { field: 'Address', title: '设备地址', width: 200 }
                , {
                    field: "operation", title: "操作", width: 150,
                    templet: function (d) {
                        return '' +
                            '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(UserDeviceTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除该设备？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Panel/UserDeviceDelete",
                            async: false,
                            data: { "DeviceId": data.DeviceId,"UserId":SelUserId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }
        });

        table.render({
            elem: '#demo2'
            , method: 'post'
            //, height: 315
            , url: '/Panel/DeviceTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "DeviceId", Order: "Desc", Condition: " and IsDelete<>1" }
            , limits: [10, 50, 100]

            , cols: [[ //表头
                {type:"radio"}
                , { field: 'Name', title: '设备名称', width: 200 }
                , { field: 'Address', title: '设备地址', width: 200 }
                , { field: 'ChannelNum', title: '通道数', width: 100 }

            ]]
        });





    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo2", {
                where: {
                    SortName: "DeviceId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo2", {
                where: {
                    SortName: "DeviceId", Order: "Desc", Condition: " and IsDelete<>1 and Name like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }


</script>
